package com.chaoyi.util.oss;

import java.io.InputStream;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;

public class ApkOSSUpload {
	private InputStream fileContent;
	private long contentLength;
	private String fileName;
	private String remotePath;
	private ApkOSSConfigure configure;
	
	public static ApkOSSUpload custom(){
		return new ApkOSSUpload();
	}
	
	public ApkOSSUpload setFileContent(InputStream fileContent) {
		this.fileContent = fileContent;
		return this;
	}
	
	public ApkOSSUpload setContentLength(long contentLength) {
		this.contentLength = contentLength;
		return this;
	}
	
	public ApkOSSUpload setFileName(String fileName) {
		this.fileName = fileName;
		return this;
	}
	
	public ApkOSSUpload setRemotePath(String remotePath) {
		this.remotePath = remotePath;
		return this;
	}
	
	public ApkOSSUpload setConfigure(ApkOSSConfigure configure) {
		this.configure = configure;
		return this;
	}

	public String upload(){
		OSSClient ossClient = new OSSClient(configure.getEndpoint(), configure.getAccessKeyId(),
				configure.getAccessKeySecret());

		// 创建上传Object的Metadata
		ObjectMetadata objectMetadata = new ObjectMetadata();
		objectMetadata.setContentLength(contentLength);
		objectMetadata.setCacheControl("max-age=86400");
		objectMetadata.setHeader("Pragma", "max-age=86400");
		objectMetadata.setContentType("application/vnd.android.package-archive");
		objectMetadata.setContentDisposition("inline;filename=" + fileName);

		// 执行 put 请求（上传文件），并且返回对象的MD5摘要
		ossClient.putObject(configure.getBucketName(), remotePath+fileName, fileContent, objectMetadata);
		System.out.println(configure.getAccessUrl() + "/" + remotePath+fileName);
		return configure.getAccessUrl() + "/" + remotePath+fileName;
	}
	
	
}
